package com.shujia.duotai.instanceof_;

import org.junit.Test;

public class PersonTest {

    @Test
    public void test1() {
    }

    public static void main(String[] args) {

        // instanceof 和强转配合使用, ClassCastException
        Person person = new Student();

        // 判断person 对象是否是Teacher类型的实例
        boolean flag = person instanceof Teacher;
        System.out.println(flag);
        if (flag) {
            Teacher teacher = (Teacher) person;
            System.out.println(teacher);
        }

        flag = person instanceof Student;
        System.out.println(flag);
        if (flag) {
            Student student = (Student) person;
            System.out.println(student);
        }

        //判断对象是否是右边类型的子类的实例
        System.out.println(person instanceof Person);
        // 没有涉及到强转
        // Person p1 = person;

        System.out.println(person instanceof Worker);

        if (person instanceof Worker) {
            Worker worker = (Worker) person;
        }


    }
}
